package com.hengda.museumonline.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.ExlistBean;
import com.hengda.museumonline.ui.MuseumContentActivity;
import com.hengda.museumonline.utils.GlideCircleTransform;

import static com.yolanda.nohttp.NoHttp.getContext;

/**
 * @author：JFWU on 2016/9/26 18:36
 * @E-mail：jeffy12138@126.com
 */
public class MuseumAdapter extends RecyclerView.Adapter<MuseumAdapter.ListHolder> {
    private Context context;
    private ExlistBean exlistBean = new ExlistBean();

    public MuseumAdapter(Context context, ExlistBean exlistBean) {
        this.context = context;
        this.exlistBean = exlistBean;
    }

    @Override
    public MuseumAdapter.ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.museum_item, parent, false);

        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(MuseumAdapter.ListHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return exlistBean.getData().size();
    }

    class ListHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView days;
        private TextView source;
        private ImageView itemBackground;
        private CardView museum_item;

        public ListHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            days = (TextView) itemView.findViewById(R.id.days);
            source = (TextView) itemView.findViewById(R.id.source);
            itemBackground = (ImageView) itemView.findViewById(R.id.item_background);
            museum_item = (CardView) itemView.findViewById(R.id.museum_item);
        }

        public void setData(final int position) {
            if (exlistBean.getData().size() > 0) {
                title.setText(exlistBean.getData().get(position).getTitle());
                days.setText(exlistBean.getData().get(position).getDays() + "天后结束");
                source.setText(exlistBean.getData().get(position).getSource());
                Glide.with(getContext()).load(exlistBean.getData().get(position).getItem_background()).transform(new GlideCircleTransform(getContext())).into(itemBackground);
                museum_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", exlistBean.getData().get(position).getObject_id());
                        Intent intent = new Intent(getContext(), MuseumContentActivity.class);
                        intent.putExtras(bundle);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        getContext().startActivity(intent);
                    }
                });
            }
        }
    }
}
